package com.serkol.abmanager.controller.api;

import com.serkol.abmanager.bean.GridResult;
import com.serkol.abmanager.bean.Result;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.Automobiles;
import com.serkol.abmanager.entity.Departments;
import com.serkol.abmanager.service.AutomobilesService;
import com.serkol.abmanager.service.DepartmentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/departments")
@Api(tags = "部门表")
@ResponseBody
public class DepartmentsApiController {

    @Autowired
    private DepartmentsService service;
    @ApiOperation(value = "分页查询")
    @PostMapping("/findByList")
    public GridResult findByList(@RequestParam(name = "name",defaultValue = "") String name,@RequestParam(name = "manager",defaultValue = "") String manager,@RequestParam(name = "page",defaultValue = "0") int page, @RequestParam(name = "pagesize",defaultValue = "10") int pagesize){

        GridResult page1 = service.findByList(page,pagesize,name,manager);

        return  page1;
    }
    @ApiOperation(value = "查询")
    @PostMapping("/findById")
    public Result findById(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            Departments byId = service.getById(id);
            if (byId != null) {
                return Result.ok(byId);
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }
@ApiOperation(value = "删除")
    @PostMapping("/findByDelete")
    public Result findByDelete(@RequestParam(name = "id", defaultValue = "0") long id) {
        if (id > 0) {
            boolean b = service.removeById(id);
            if (b) {
                return Result.ok();
            } else {
                return Result.failure(ResultConstant.EMPTIY);
            }
        } else {
            return Result.failure();
        }


    }
@ApiOperation(value = "添加或编辑")
    @PostMapping("/edit")
    public Result edit(@RequestParam(name = "id", defaultValue = "0") long id,
                       @RequestParam(name = "name", defaultValue = "") String name,
                       @RequestParam(name = "address", defaultValue = "") String address,
                       @RequestParam(name = "manager", defaultValue = "") String manager,
                       @RequestParam(name = "groupId", defaultValue = "") long groupId,
                       @RequestParam(name = "tell", defaultValue = "") String tell)
    {
        Result result = new Result();
        if (!name.isEmpty() && !address.isEmpty() && !manager.isEmpty() && !tell.isEmpty()) {
            Departments goods=new Departments();
            goods.setName(name);
            goods.setAddress(address);
            goods.setManager(manager);
            goods.setTell(tell);
            goods.setGroupId(groupId);
            boolean flag = false;
            if (id > 0) {
                goods.setId(id);
                flag = service.updateById(goods);
            } else {
                flag = service.save(goods);
            }
            if (flag) {
                result.setStatus(ResultConstant.SUCSESS);
            } else {
                result.setStatus(ResultConstant.FAIL);
            }
        } else {
            result.setMsg("不能为空");
        }

        return result;

    }
}
